/**
 * Project Name:MeatballUtils.java
 * File Name:MeatballUtils.java
 * Date:2019/3/7 14:51
 * Copyright (c) 2019, zhang.xiangyu@foxmail.com All Rights Reserved.
 */
package com.meatball.utils;

import com.meatball.base.repository.domain.Params;
import com.mongodb.QueryBuilder;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Title: MeatballUtils.java
 * @Description: todo(基礎工具類)
 * @Author: 張翔宇
 * @Date: 2019/3/7 14:51
 * @Version: V1.0
 */
@Log4j2
public class MeatballUtils {

    /**
     * @title: mapToObject
     * @description: todo(Map轉Bean)
     * @params: [map, beanClass]
     * @return: T    返回类型
     * @author: 張翔宇
     * @date: 2019/3/7 15:08
     */
    public static <T, K, V> T mapToObject(Map<K, V> map, Class<T> beanClass) {
        if (map == null) {
            return null;
        }
        T obj = null;
        try {
            obj = beanClass.newInstance();
            BeanUtils.populate(obj, map);
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    /**
     * @title: objectToMap
     * @description: todo(Bean 轉 Map)
     * @params: [obj]
     * @return: java.util.Map<?,?>    返回类型
     * @author: 張翔宇
     * @date: 2019/3/7 15:12
     */
    public static <T, K, V>Map<K, V> objectToMap (T obj) {
        if (obj == null) return null;
        return new BeanMap(obj);
    }

    /**
     * @title: getDocumentValue
     * @description: todo(获取当前需查询表名称)
     * @params: [entityClass]
     * @return: java.lang.String    返回类型
     * @author: 張翔宇
     * @date: 2019/4/4 17:53
     */
    public static <T>String getTableName (Class<T> entityClass) {
        String name;
        if (null != entityClass.getAnnotation(Document.class)) {
            name = entityClass.getAnnotation(Document.class).value();
        } else {
            name = entityClass.getSimpleName();
            name = (new StringBuilder()).append(Character.toLowerCase(name.charAt(0))).append(name.substring(1)).toString();
        }
        log.info("欲查询表名称：{}", name);
        return name;
    }

    /**
     * @title: params
     * @description: todo(格式化请求参数)
     * @params: [t]
     * @return: com.meatball.base.repository.domain.Params    返回类型
     * @author: 張翔宇
     * @date: 2019/4/4 22:26
     */
//    public static <T>Params formatQuery (T t) {
//        List<AggregationOperation> operations = new ArrayList<>();
//        QueryBuilder queryBuilder = new QueryBuilder();
//        Class<?> tClass = t.getClass();
//        Field[] field = (Field[]) ArrayUtils.addAll(tClass.getDeclaredFields(), tClass.getSuperclass().getDeclaredFields());
//        for (Field info : field) {
//            info.setAccessible(true);
//            try {
//                if (null != info.get(t)) {
//                    log.info("参数名称：{}，参数值：{}", info.getName(), info.get(t));
//                    operations.add(Aggregation.match(Criteria.where(info.getName()).is(info.get(t))));
//                    queryBuilder.and(info.getName()).is(info.get(t));
//                }
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            }
//        }
//        return new Params(operations, new BasicQuery(queryBuilder.get().toString()));
//    }
}
